﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using KerriganInterface;
using KerriganDataContract;
using KerriganInterface.IServiceClient;
using System.Web.Security;

namespace KerriganForum
{
    public class ServiceProxy : IDisposable     
    {
        #region Properties
        public ChannelFactory<IForumService> MyServiceFactory { get; set; }
        public IForumService Service { get; set; }
        public bool isValidTicket { get; set; } 
        #endregion

        public ServiceProxy()
        {
            MyServiceFactory = new ChannelFactory<IForumService>(@"ForumService");

            MyServiceFactory.Opened += new EventHandler(myServiceFactory_Opened);
            MyServiceFactory.Opening += new EventHandler(myServiceFactory_Opening);
            MyServiceFactory.Closing += new EventHandler(myServiceFactory_Closing);
            MyServiceFactory.Closed += new EventHandler(myServiceFactory_Closed);

            this.Service = MyServiceFactory.CreateChannel();
        }

        protected void Initialization()
        {
            MyServiceFactory = new ChannelFactory<IForumService>(@"ForumService");
            MyServiceFactory.Opened += new EventHandler(myServiceFactory_Opened);
            MyServiceFactory.Opening += new EventHandler(myServiceFactory_Opening);
            MyServiceFactory.Closing += new EventHandler(myServiceFactory_Closing);
            MyServiceFactory.Closed += new EventHandler(myServiceFactory_Closed);
        } 


        //Methode vérifie que c'est un admin(identité)

        public ServiceProxy(HttpCookieCollection CookieCollection)
        {
            Initialization();

            FormsAuthenticationTicket ticket;

            try
            {
                HttpCookie cookie = CookieCollection.Get("BXPXAUTHFWCF");
                ticket = FormsAuthentication.Decrypt(cookie.Value.ToString());

                if (ticket.UserData != "" || ticket.UserData != null)
                {
                    string[] myArray = ticket.UserData.Split(',');
                  //  this.MyServiceFactory.Credentials.UserName.UserName = myArray[0].ToString();
                  //  this.MyServiceFactory.Credentials.UserName.Password = myArray[1].ToString();
                    this.Service = MyServiceFactory.CreateChannel();
                    isValidTicket = true;
                }
                else
                {
                    isValidTicket = false;
                }
            }
            catch (Exception ex)
            {
                isValidTicket = false;
                System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
            }
        }

        //Méthode pour authentifier l'admin

        public ServiceProxy(string username, string password)
        {
            Initialization();

            //this.MyServiceFactory.Credentials.UserName.UserName = username;
            //this.MyServiceFactory.Credentials.UserName.Password = password;
            this.Service = MyServiceFactory.CreateChannel();
        }



        #region LifeCycle tracking
        void myServiceFactory_Closing(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Service Closing");
        }

        void myServiceFactory_Closed(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Service Closed");
        }

        void myServiceFactory_Opening(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Service Opening");
        }

        void myServiceFactory_Opened(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Service Opened");
        } 
        #endregion

        #region IDisposable Members
        public void Dispose()
        {
            try
            {
                if (MyServiceFactory != null)
                {
                    if (MyServiceFactory.State != CommunicationState.Faulted)
                    {
                        MyServiceFactory.Close();
                    }
                    else
                    {
                        MyServiceFactory.Abort();
                    }
                }
            }
            catch (CommunicationException ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
                MyServiceFactory.Abort();
            }
            catch (TimeoutException ex )
            {
                System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
                MyServiceFactory.Abort();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
                MyServiceFactory.Abort();
                throw;
            }
            finally
            {
                MyServiceFactory = null;
            }
        }
        #endregion
    }
}
